package com.yunsc.admin.controller.market;

import com.yunsc.common.utils.PageResult;
import com.yunsc.common.utils.Result;
import com.yunsc.common.utils.SecurityUtil;
import com.yunsc.common.validator.ValidationGroups;
import com.yunsc.store.dto.CouponParamDTO;
import com.yunsc.store.dto.CouponReceiveDTO;
import com.yunsc.store.entity.YunscCouponEntity;
import com.yunsc.store.entity.YunscUserCouponEntity;
import com.yunsc.store.service.YunscCouponService;
import com.yunsc.store.service.YunscUserCouponService;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @version: 1.0
 * @Author: adan
 * @description:
 * @date: 2024-06-03 18:57
 */
@RestController
@RequestMapping("/market/coupon")
public class CouponController {

    @Resource
    private YunscCouponService couponService;
    @Resource
    private YunscUserCouponService userCouponService;

    // list
    @ApiOperation(value = "获取优惠券列表", notes = "获取优惠券列表")
    @GetMapping("/list")
    public Result<?> queryCouponList(CouponParamDTO param) {
        Integer storeId = SecurityUtil.getCurrentStoreId();
        PageResult<YunscCouponEntity> coupons = couponService.queryCouponPageList(param, storeId);
        return Result.resultPageList(coupons);
    }

    // /coupon/detail

    // 获取优惠券详情
    @ApiOperation(value = "获取优惠券详情", notes = "获取优惠券详情")
    @GetMapping("/detail")
    public Result<?> queryCouponDetail(@RequestParam("couponId") Integer couponId) {
        YunscCouponEntity coupon = couponService.queryCouponDetail(couponId);
        Map<String, Object> map = new HashMap<>();
        map.put("detail", coupon);
        return Result.ok(map);
    }

    // /coupon/receive 获取优惠券列表记录
    @ApiOperation(value = "获取优惠券列表记录", notes = "获取优惠券列表记录")
    @GetMapping("/receive")
    public Result<?> queryCouponReceiveList(CouponReceiveDTO param) {
        Integer storeId = SecurityUtil.getCurrentStoreId();
        PageResult<YunscUserCouponEntity> coupons = userCouponService.queryCouponReceivePageList(param, storeId);
        return Result.resultPageList(coupons);
    }

    // /coupon/add 新增优惠券
    @ApiOperation(value = "新增优惠券", notes = "新增优惠券")
    @PostMapping("/add")
    public Result<?> addCoupon(@RequestBody CouponParamDTO param) {
        Integer storeId = SecurityUtil.getCurrentStoreId();
        couponService.addCoupon(param, storeId);
        return Result.ok("新增优惠券成功");
    }

    // /coupon/edit 修改优惠券
    @ApiOperation(value = "修改优惠券", notes = "修改优惠券")
    @PostMapping("/edit")
    public Result<?> editCoupon(@RequestBody @Validated(ValidationGroups.Update.class)  CouponParamDTO param) {
        couponService.editCoupon(param);
        return Result.ok("修改优惠券成功");
    }

    // /coupon/delete 删除优惠券
    @ApiOperation(value = "删除优惠券", notes = "删除优惠券")
    @PostMapping("/delete")
    public Result<?> deleteCoupon(@RequestBody @Validated(ValidationGroups.Delete.class)  CouponParamDTO param) {
        couponService.deleteCoupon(param);
        return Result.ok("删除优惠券成功");
    }




}
